﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using TestOnlineProject.Models.Entities;

namespace TestOnlineProject.Models.DataAccess
{
    public class TestDAO
    {
        TestOnlineDBEntities db = new TestOnlineDBEntities();

        public int Insert(Test test)
        {
            try
            {
                db.Tests.AddObject(test);
                int result = db.SaveChanges();
                return result;
            }
            catch (Exception)
            {
                throw;
            }
        }

        public int Update(Test test)
        {
            try
            {
                var oldTest = db.Tests.Where(t => t.TestId == test.TestId).FirstOrDefault();
                oldTest.Title = test.Title;
                oldTest.Duration = test.Duration;
                oldTest.Expires = test.Expires;
                oldTest.IsActivated = test.IsActivated;
                int result = db.SaveChanges();
                return result;
            }
            catch (Exception)
            {
                throw;
            }
        }

        public int Delete(int testId)
        {
            try
            {
                var test = db.Tests.Where(t => t.TestId == testId).FirstOrDefault();
                foreach (var ut in test.UserTests)
                {
                    ut.UserAnswers.ToList().ForEach(db.DeleteObject);
                }
                test.UserTests.ToList().ForEach(db.DeleteObject);
                test.TestQuestions.ToList().ForEach(db.DeleteObject);
                db.Tests.DeleteObject(test);
                var result = db.SaveChanges();
                return result > 0 ? 1 : 0;
            }
            catch (Exception)
            {
                throw;
            }
        }

        public List<Test> SelectBySubjectId(int subjectId)
        {
            try
            {
                var tests = db.Tests.Where(t => t.SubjectId == subjectId).ToList();
                return tests;
            }
            catch (Exception)
            {
                throw;
            }
        }

        public List<Test> Select()
        {
            try
            {
                var tests = db.Tests.ToList();
                return tests;
            }
            catch (Exception)
            {
                throw;
            }
        }

        public Test SelectByTestId(int testId)
        {
            try
            {
                var test = db.Tests.Where(t => t.TestId == testId).FirstOrDefault();
                return test;
            }
            catch (Exception)
            {
                throw;
            }
        }

        public int DeleteByUserId(int userId)
        {
            try
            {
                var result = 0;
                var tests = db.Tests.Where(x => x.UserId == userId).ToList();
                foreach (var test in tests)
                {
                    var testQuestions = db.TestQuestions.Where(x => x.TestId == test.TestId).ToList();
                    foreach (var testQuestion in testQuestions)
                    {
                        db.TestQuestions.DeleteObject(testQuestion);
                    }
                    db.Tests.DeleteObject(test);
                    result += db.SaveChanges();
                }
                return result;
            }
            catch (Exception)
            {
                return 0;
            }
        }
    }
}